package com.zhao.controller;

import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.zhao.dao.EbickDao;
import com.zhao.pojo.Admin;
import com.zhao.pojo.SchoolCounsellor;
import com.zhao.pojo.Student;
import com.zhao.pojo.User;
import com.zhao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class PublicControllor {

    @Autowired
    private PublicizeService publicizeService;

    @Autowired
    private DrivingLicenseService drivingLicenseService;

    @Autowired
    private EbickGoodsService ebickGoodsService;

    @Autowired
    private SuperviseService superviseService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private WalletService walletService;

    @Autowired
    private OrderFormService orderFormService;

    //清理session项目
    @RequestMapping("clearMessage.do")
    public String clearMessage(String sessionItem, String from ,HttpSession session){

        System.out.println("清理"+sessionItem);
        System.out.println("from "+from);
        session.removeAttribute(sessionItem);

        return "redirect:/"+from;
    }

    @RequestMapping("toStudentIndex.do")
    public String toStudentIndex(HttpSession session){

        Student student =(Student) session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = publicizeService.getNew3Publicize(session,student.getUniversityID());

        if(!res.equals("success")){
            return "404";
        }

        ///查询行驶证
        System.out.println("查看自己的行驶证明");
        res = drivingLicenseService.getDrivingLicenseImpl(session);
        System.out.println(res);
        if(res.equals("没有行驶证")){
            session.setAttribute("getDrivingLicenseMessage","没有行驶证");
        }
        //获取主页数据
        student =(Student) session.getAttribute("student");
        ebickGoodsService.getEbickGoodsNum(student.getUniversityID(),session);
        superviseService.getSupervisesNum(student,session);
        studentService.getStudentNum(student.getUniversityID(),session);
        //获取主页钱包
        walletService.getMyWallet(student,session);
        //获取商品信息
        ebickGoodsService.studentSeeAllEbickGoods(student,session);
        //获取举报
        superviseService.getSuperviseMe(session);

        return "redirect:/studentIndex";
    }

    @Autowired
    private EbickService ebickService;

    @RequestMapping("toSchoolCounsellorIndex.do")
    public String toSchoolCounsellorIndex(HttpSession session){

        SchoolCounsellor schoolCounsellor =(SchoolCounsellor) session.getAttribute("schoolCounsellor");
        if(schoolCounsellor == null){
            return "redirect:/";
        }
        String res = publicizeService.getNew3Publicize(session,schoolCounsellor.getUniversityID());

        if(!res.equals("success")){
            return "404";
        }
        //获取主页数据
        schoolCounsellor =(SchoolCounsellor) session.getAttribute("schoolCounsellor");
        //ebickGoodsService.getEbickGoodsNum(schoolCounsellor.getUniversityID(),session);
        superviseService.getSupervisesNum(schoolCounsellor,session);
        orderFormService.schoolCounsellorGetFinishedOrderNum(schoolCounsellor,session);
        //studentService.getStudentNum(schoolCounsellor.getUniversityID(),session);
        ebickService.getMyStudentEbickNum(schoolCounsellor,session);
        return "redirect:/schoolCounsellorIndex";
    }

    @RequestMapping("toAdminIndex.do")
    private String toAdminIndex(HttpSession session){

        Admin admin = (Admin) session.getAttribute("admin");
        //ebickGoodsService.getEbickGoodsNum(admin.getUniversityID(),session);
        superviseService.getSupervisesNum(admin.getUniversityID(),session);
        //studentService.getStudentNum(admin.getUniversityID(),session);
        ebickService.adminGetAllEbickNum(admin,session);
        orderFormService.adminGetFinishenOrdersNum(admin,session);
        ///查询最新公告
        String res = publicizeService.getNew3Publicize(session,admin.getUniversityID());
        System.out.println(res);
        if(!res.equals("success")){
            System.out.println("get new 3 error");
        }
        return "redirect:/adminIndex";
    }

}
